package org.example.rowing.service;

import lombok.extern.slf4j.Slf4j;
import org.example.rowing.domain.Event;
import org.example.rowing.domain.Team;
import org.example.rowing.enums.BoatTypeEnum;
import org.example.rowing.mapper.AthleteMapper;
import org.example.rowing.mapper.EventMapper;
import org.example.rowing.mapper.TeamMapper;
import org.example.rowing.vo.EventVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class EventInfoService {

    @Autowired
    private EventMapper eventMapper;
    @Autowired
    private TeamMapper teamMapper;
    @Autowired
    private AthleteMapper athleteMapper;

    public List<String> getAllEventNames() {
        return eventMapper.getAllEventNames();
    }

    public List<EventVO> showEventsByType(BoatTypeEnum boatTypeEnum) {
        Event eventCondition = new Event();
        eventCondition.setBoatTypeEnum(boatTypeEnum);
        List<Event> events = eventMapper.selectEventByConditions(eventCondition);
        log.info("目前查询到{}条赛事信息", events.size());
        List<EventVO> eventVOS = new ArrayList<>();
        for (Event event : events) {
            EventVO eventVO = new EventVO(event.getId(), event.getName(),
                    event.getStartDate(), event.getEndDate(), event.getLocation(),
                    boatTypeEnum.getDisplayName());
            eventVOS.add(eventVO);
        }
        return eventVOS;
    }

    public void saveOrUpdateEvent(Event event) {
        if (event.getId() == null) {
            eventMapper.insertEvent(event);
            log.info("新赛事已保存: {}", event);
        } else {
            eventMapper.updateEvent(event);
            log.info("赛事已更新: {}", event);
        }
    }

    public void deleteEvent(Long id) {
        if (eventMapper.selectEventById(id) != null) {
            // 1.删除指定赛事的队伍的队员信息
            Team teamCondition = new Team();
            teamCondition.setEventId(id);
            List<Team> teams = teamMapper.selectTeamByConditions(teamCondition);
            for (Team team : teams) {
                athleteMapper.deleteAthletesByTeamId(team.getId());
            }
            // 2.删除指定赛事下的队伍信息
            teamMapper.deleteTeamsByEventId(id);
            // 3.删除这个赛事信息
            eventMapper.deleteEvent(id);
            log.info("赛事已删除: {}", id);
        } else {
            log.info("此赛事暂不存在!!!");
        }
    }

    public List<EventVO> showSearchedEvents(Event eventConditions) {
        List<EventVO> eventVOS = new ArrayList<>();
        if (eventConditions != null) {
            List<Event> events = eventMapper.selectEventByConditions(eventConditions);
            log.info("目前查询到{}条赛事信息", events.size());
            for (Event event : events) {
                EventVO eventVO = new EventVO(event.getId(), event.getName(), event.getStartDate(),
                        event.getEndDate(), event.getLocation(), event.getBoatTypeEnum().getDisplayName());
                eventVOS.add(eventVO);
            }
        }
        return eventVOS;
    }
}
